package com.chenglu.android.mine.beans;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chenglu.android.R;
import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BaseActionBarActivity;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.mine.bean.AmountTypeBean;
import com.chenglu.android.mine.bean.BeansDetailBean;
import com.chenglu.android.mine.bean.UserInfoBean;
import com.chenglu.android.mine.beans.adapter.BeansRecordAdapter;
import com.chenglu.android.mine.beans.mvp.contract.BeansContract;
import com.chenglu.android.mine.beans.mvp.presenter.BeansPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yalantis.ucrop.util.ScreenUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: lyj
 * @Created: by Copyright 2023 6 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：
 */
public class BeansRecordActivity extends BaseActionBarActivity<BeansPresenter> implements BeansContract.View {

     public  static  final  String AMOUNTTYPE = "AMOUNTTYPE";
    @BindView(R.id.rv_beans)
    RecyclerView rvBeans;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    @BindView(R.id.ll_loading_data)
    LinearLayout llLoadingData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_loading_no_data)
    LinearLayout llLoadingNoData;
    private int typeName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_beans_record;
    }

    int current = 1;
    private BeansRecordAdapter beansRecordAdapter;

    @Override
    protected void initView() {

        typeName = getIntent().getExtras().getInt(AMOUNTTYPE);

        mPresenter = new BeansPresenter();
        mPresenter.attachView(this);
        switch (typeName){
            case 1:
                setCenterText("余额记录");
                break;
            case 2:
                setCenterText("积分记录");
                break;
            case 3:
                setCenterText("红豆记录");
                break;
            default:
                break;
//            case 5:
//                setCenterText("商家红包余额 ");
//                break;
//            case 6:
//                setCenterText("平台红包余额");
//                break;
        }

        mPresenter.getAmountTypeList(getToken(),getMapParams());
        initAdapter();
        initRefreshLayout();

    }

    //刷新
    private void initRefreshLayout() {

        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //id 需要传参过来
                current = 1;
                //需要清空数据再加
                list.clear();
                mPresenter.getAmountTypeList(getToken(),getMapParams());


            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                current += 1;
                mPresenter.getAmountTypeList(getToken(),getMapParams());

            }
        });
    }

    List<AmountTypeBean.Data.Records> list = new ArrayList<>();

    private void initAdapter() {
        beansRecordAdapter = new BeansRecordAdapter(R.layout.item_beanslist, list);
        rvBeans.setLayoutManager(new LinearLayoutManager(mContext));
        rvBeans.addItemDecoration(new SpaceItemDecoration(2, ScreenUtils.dip2px(mContext,16),true));
        rvBeans.setAdapter(beansRecordAdapter);
//        commentAdapter.notifyDataSetChanged();
    }

    private Map<String, String> getMapParams() {
        Map<String, String> map = new HashMap<>();
        map.put("amountType", typeName +"");
        map.put("current", current + "");
        map.put("size", Constant.SIZE + "");
//        map.put("sourceType","");
//        map.put("type",amountType);

        return map;
    }

    @Override
    public void getActiveBeans(SmsCodeBean bean) {

    }

    @Override
    public void getAmountTypeList(AmountTypeBean bean) {

         stopRefresh(smartrefreshlayout);
        if (bean.getData().getRecords().size() > 0) {
            llLoadingData.setVisibility(View.VISIBLE);
            llLoadingNoData.setVisibility(View.GONE);

            for (int i = 0; i < bean.getData().getRecords().size(); i++) {
                //添加集合中banner的数据
                list.add(bean.getData().getRecords().get(i));
            }

            if(bean.getData().getTotal() < Constant.SIZE){
                smartrefreshlayout.setEnableRefresh(false);
            }
            if(current *Constant.SIZE > bean.getData().getTotal()){
                smartrefreshlayout.setEnableLoadMore(false);
                View inflate = Global.inflate(R.layout.loadmore_addfooter_layout);
                beansRecordAdapter.addFooterView(inflate);
            }else {
                smartrefreshlayout.setEnableLoadMore(true);
                beansRecordAdapter.removeAllFooterView();
            }

            beansRecordAdapter.notifyDataSetChanged();
        } else {
            if (list.isEmpty()) {
//                showEmptyView("暂无数据");
                llLoadingData.setVisibility(View.GONE);
                llLoadingNoData.setVisibility(View.VISIBLE);
            }

        }
    }

    @Override
    public void getUserInfo(UserInfoBean bean) {

    }

    @Override
    public void getBeansDetails(BeansDetailBean bean) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    class SpaceItemDecoration extends RecyclerView.ItemDecoration {

        private int spanCount; //列数
        private int spacing; //间隔
        private boolean includeEdge; //是否包含边缘

        public SpaceItemDecoration(int spanCount, int space, boolean includeEdge) {
            this.spacing = space;
            this.spanCount = spanCount;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
//            outRect.bottom = spacing;
            outRect.top = ScreenUtils.dip2px(mContext,26);
            outRect.left = spacing;
            outRect.right = spacing;

//            //获取当前Item的位置
//            int position = parent.getChildAdapterPosition(view); // item position
//            //判断奇偶位置，然后进行相应的赋值运算
//            int column = position % spanCount; // item column
//            if (column == 0){
//                outRect.left = spacing;
//                outRect.right = spacing /spanCount;
//            }else if (column == 1){
//                outRect.left = spacing /spanCount;
//                outRect.right = spacing;
//            }

        }
    }

}
